<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
       if(!IS_POST || !IS_AJAX )  {
           $this->display('index');
       }else{
          $data['username']=trim(I('post.UserName'));
          $data['password']=trim(I('post.UserPwd'));
          $vercode=trim(I('post.vercode'));
             if(!$vercode || !$data['username'] || !$data['password'] || strlen($data['username']) >12 || strlen($data['username'])<4) {
                $data2['Msg']="用户名或者密码错误，请重新输入";
                $this->ajaxReturn($data2);
             }
              if(!check_verify ($vercode)){
                  $data2['Msg']="验证码输入有误，请重新输入";
                  $this->ajaxReturn($data2);
              }
              $user=M('user')->where(array('username'=>$data['username']))->find();        
              if($user['status']== 0){
                 $data2['Msg']="账户禁用";
                 $this->ajaxReturn($data2);
              }
              if(!$user || $user['password'] <> md5($data['password'])){
                $data2['Msg']="用户名或者密码错误，请重新输入";
                $this->ajaxReturn($data2);
              }elseif($user['status']=='0'){
                $data2['Msg']="用户名或者密码错误，请重新输入2";
                $this->ajaxReturn($data2);
              }else{
                  session('uid',$user[id]);
                  session('logintime',time());
                  session('logintime2',time());
                  session('username',$user['username']);
                  session('nicename',$user['nicename']);
                  session('UserMoney',$user['monery']);
                  session('sumMoney',0);
                  $list=M('user')->where(array('tid'=>$_SESSION ['uid']))->count();
                  session('ref',$list);

                  $record1['date']= date ( 'Y-m-d H:i:s', time () );
                  $record1['ip'] = get_client_ip();
                  $record1['uid'] = $_SESSION ['uid'];
                  M ('denglu')->add ( $record1 );
                  unset( $record1);
                  $data2['Success']=1;
                  $this->ajaxReturn($data2);

              }
              $data2['Msg']="登录错误";
              $this->ajaxReturn($data2);

       } 
    }
    public function Loginout(){
      session(null);
      session_unset();
      session_destroy();
      $this->redirect('/');
    }

}
